// components/image-upload/image-upload.js
Component({
  properties: {
    maxCount: {
      type: Number,
      value: 1
    },
    imageType: {
      type: String,
      value: 'other'
    }
  },

  data: {
    imageList: []
  },

  methods: {
    chooseImage: function() {
      const that = this;
      const maxCount = this.data.maxCount - this.data.imageList.length;
      
      if (maxCount <= 0) {
        wx.showToast({
          title: '已达到最大上传数量',
          icon: 'none'
        });
        return;
      }
      
      wx.chooseImage({
        count: maxCount,
        sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function(res) {
          const tempFilePaths = res.tempFilePaths;
          
          // 上传图片
          that.uploadImages(tempFilePaths);
        },
        fail: function() {
          wx.showToast({
            title: '选择图片失败',
            icon: 'none'
          });
        }
      });
    },

    uploadImages: function(filePaths) {
      const that = this;
      const app = getApp();
      const imageList = this.data.imageList;
      
      // 逐个上传图片
      filePaths.forEach((filePath, index) => {
        wx.showLoading({
          title: `上传中(${index + 1}/${filePaths.length})`
        });
        
        // 上传到服务器
        wx.uploadFile({
          url: `${app.globalData.baseUrl}/common/upload/`,
          filePath: filePath,
          name: 'image',
          header: {
            'Authorization': `Bearer ${app.globalData.token}`
          },
          formData: {
            'image_type': this.data.imageType
          },
          success: function(res) {
            if (res.statusCode === 201) {
              const data = JSON.parse(res.data);
              imageList.push(data.image_url);
              that.setData({
                imageList: imageList
              });
              
              // 触发上传完成事件
              that.triggerEvent('uploadsuccess', {
                imageUrl: data.image_url,
                imageId: data.id
              });
            } else {
              wx.showToast({
                title: '上传失败',
                icon: 'none'
              });
            }
          },
          fail: function() {
            wx.showToast({
              title: '上传失败',
              icon: 'none'
            });
          },
          complete: function() {
            wx.hideLoading();
          }
        });
      });
    },

    deleteImage: function(e) {
      const index = e.currentTarget.dataset.index;
      const imageList = this.data.imageList;
      const deletedImage = imageList[index];
      
      imageList.splice(index, 1);
      this.setData({
        imageList: imageList
      });
      
      // 触发删除事件
      this.triggerEvent('deleteimage', {
        imageUrl: deletedImage,
        index: index
      });
    },

    // 获取已上传的图片URL列表
    getImageUrls: function() {
      return this.data.imageList;
    },

    // 清空图片列表
    clearImages: function() {
      this.setData({
        imageList: []
      });
    }
  }
})